/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.webbapps.cormac_susilo.core;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 * The frame registry implements methods for model/database interactions
 * on the frame Entity
 * @author tor
 */
public final class FrameRegistry extends AbstractDAO<Frame, Long>{
    
    private FrameRegistry(String puName) {
        super(Frame.class, puName);
    }
    
    public static FrameRegistry newInstance(String puName) {
        return new FrameRegistry(puName);
    }
    /**
     * Get a list of Frames with the given run_id in ascending time_label order.
     * @param run_id the run id constraint
     * @return a list of Frame in ascending time_label order.
     */
    public List<Frame> getFrames(long run_id) {
        
        EntityManager em = super.getEM();
        
        CriteriaBuilder cb  = em.getCriteriaBuilder();
        CriteriaQuery<Frame> cq = cb.createQuery(Frame.class);
        Root<Frame> frame = cq.from(Frame.class);
        cq.select(frame);
        cq.where(cb.equal(frame.get(Frame_.run).get(Run_.id), run_id));
        cq.orderBy(cb.asc(frame.get(Frame_.time_label)));

        List<Frame> result = em.createQuery(cq).getResultList();
        em.close();
        
        return result;
    }
}
